package edu.nju.cineplex.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import edu.nju.cineplex.dao.DaoHelper;
import edu.nju.cineplex.dao.TimetableDao;
import edu.nju.cineplex.factory.DaoFactory;
import edu.nju.cineplex.model.Timetable;

public class TimetableDaoImpl implements TimetableDao {
	private static TimetableDaoImpl timetableDao = new TimetableDaoImpl();
	private static DaoHelper daoHelper = DaoHelperImpl.getBaseDaoInstance();

	private TimetableDaoImpl() {

	}

	public static TimetableDaoImpl getInstance() {
		return timetableDao;
	}

	@Override
	public void addTimetable(Timetable timetable) {
		// TODO Auto-generated method stub
		Connection con = daoHelper.getConnection();
		PreparedStatement stmt = null;
		try {
			stmt = con
					.prepareStatement("insert into timetable(mId,hId,startTime,createDate) values(?,?,?,?)");
			stmt.setLong(1, timetable.getMovie().getId());
			stmt.setLong(2, timetable.getHall().getId());
			stmt.setString(3, timetable.getStartTime());
			stmt.setDate(4, timetable.getCreateDate());
			stmt.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeConnection(con);
		}
	}

	@Override
	public List<Timetable> findByCreateDate(Date date) {
		// TODO Auto-generated method stub
		Connection con = daoHelper.getConnection();
		PreparedStatement stmt = null;
		ResultSet result = null;
		List<Timetable> timetableList = new ArrayList<Timetable>();
		try{
			stmt = con.prepareStatement("select * from timetable where createDate = ?");
			stmt.setDate(1, date);
			result = stmt.executeQuery();
			while(result.next()){
				Timetable timetable = new Timetable();
				timetable.setId(result.getLong(1));
				timetable.setMovie(DaoFactory.getMovieDao().findById(result.getLong(2)));
				timetable.setHall(DaoFactory.getHallDao().findById(result.getLong(3)));
				timetable.setStartTime(result.getString(4));
				timetable.setCreateDate(date);
				timetableList.add(timetable);
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			daoHelper.closeResultSet(result);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeConnection(con);
		}
		return timetableList;
	}

	@Override
	public Timetable findById(long id) {
		// TODO Auto-generated method stub
		Connection con = daoHelper.getConnection();
		PreparedStatement stmt = null;
		ResultSet result = null;
		try{
			stmt = con.prepareStatement("select * from timetable where id = ?");
			stmt.setLong(1, id);
			result = stmt.executeQuery();
			while(result.next()){
				Timetable timetable = new Timetable();
				timetable.setId(result.getLong(1));
				timetable.setMovie(DaoFactory.getMovieDao().findById(result.getLong(2)));
				timetable.setHall(DaoFactory.getHallDao().findById(result.getLong(3)));
				timetable.setStartTime(result.getString(4));
				timetable.setCreateDate(result.getDate(5));
				return timetable;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}finally{
			daoHelper.closeResultSet(result);
			daoHelper.closePreparedStatement(stmt);
			daoHelper.closeConnection(con);
		}
		return null;
	}
}
